package com.chenwang.measure.ui.dialog;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import com.chenwang.measure.R;
import com.chenwang.measure.base.BaseDialogFragment;
import com.chenwang.measure.model.bean.ServerBean;
import com.chenwang.measure.utils.MyToast;
import com.chenwang.measure.utils.SharePreferenceUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @松 on 2019/8/25 09:02
 * 添加服务器
 */
public class AddServerDialog extends BaseDialogFragment {


    Unbinder unbinder;
    @BindView(R.id.et_name)
    EditText mEtName;
    @BindView(R.id.et_ip)
    EditText mEtIp;
    @BindView(R.id.et_port)
    EditText mEtPort;
    @BindView(R.id.et_user)
    EditText mEtUser;
    @BindView(R.id.et_pass)
    EditText mEtPass;
    private I mI;


    @Override
    public int setLayoutId() {
        return R.layout.dialog_add_server;
    }

    @Override
    protected void initWindow(Window window) {
        setLayoutParams(window, Gravity.TOP, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, 0, 50);
    }

    @Override
    protected void initData(View view) {
        super.initData(view);
        unbinder = ButterKnife.bind(this, view);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }




    @OnClick({R.id.btn_cancel, R.id.btn_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_cancel:
                dismiss();
                break;
            case R.id.btn_confirm:
                add();
                break;
        }
    }

    private void add(){
        String name = mEtName.getText().toString();
        String ip = mEtIp.getText().toString();
        String port = mEtPort.getText().toString();
        String user= mEtUser.getText().toString();
        String pass = mEtPass.getText().toString();
        if (TextUtils.isEmpty(name)){
            MyToast.show("名字不能为空");
        }else if (TextUtils.isEmpty(ip)){
            MyToast.show("域名ip不能为空");
        }else if (TextUtils.isEmpty(port)){
            MyToast.show("端口个不能空");
        }else if (TextUtils.isEmpty(pass)){
            MyToast.show("密码不能空");
        }else if (TextUtils.isEmpty(user)){
            MyToast.show("用户名不能空");
        }else {
            String json ;
                   json = SharePreferenceUtils.getString(getContext(),"server_list" ,"");
            Type type = new TypeToken<List<ServerBean>>(){}.getType();
            Gson gson = new Gson();
            List<ServerBean> list = new ArrayList<>();
            if (!TextUtils.isEmpty(json)){
                 list = gson.fromJson(json, type);
            }

            ServerBean serverBean = new ServerBean();
            serverBean.setIp(ip);
            serverBean.setName(name);
            serverBean.setPassword(pass);
            serverBean.setUser(user);
            serverBean.setPort(port);
            list.add(0,serverBean);
            json = gson.toJson(list);
            SharePreferenceUtils.putString(getContext(),"server_list" , json);
            MyToast.show("添加成功");
            mI.success(serverBean);
            dismiss();


        }
    }

    public void setI(I i){
        mI=i;
    }
    public interface I{
        void success( ServerBean serverBean);
    }

}
